package com.example.actionsignin.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.actionsignin.R;
import com.example.actionsignin.adapter.ActionAdapter;
import com.example.actionsignin.entity.ActionEntity;
import com.example.actionsignin.entity.ImagesEntity;
import com.example.actionsignin.entity.UserEntity;
import com.example.actionsignin.http.HttpUtil;
import com.example.actionsignin.mvp.view.SignInActivity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static android.content.ContentValues.TAG;

/**
 * Created by 旭 on 2017/8/10/010.
 */

public class MyJoinFragment extends Fragment implements AdapterView.OnItemClickListener{

    private View view;
    private ListView mListView;

    private List<ActionEntity<UserEntity,ImagesEntity>> mList = new ArrayList<>();
    private ActionAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my_join,container,false);

        initView();

        return view;
    }

    private void initView()
    {
        mListView = (ListView) view.findViewById(R.id.fragment_my_join_list);

        mAdapter = new ActionAdapter(getActivity(),mList);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ActionEntity<UserEntity,ImagesEntity> actionEntity = mList.get(position);
        Intent intent = new Intent(getActivity(), SignInActivity.class);
        intent.putExtra("aid",actionEntity.getAid());
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    public void getData() {
        HttpUtil.onSend().getJoinAction()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ActionEntity<UserEntity,ImagesEntity>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<ActionEntity<UserEntity,ImagesEntity>> list) {
                        mList.clear();

                        for (int i = list.size()-1; i >= 0; i--) {
                            mList.add(list.get(i));
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }
}
